************************************************************************************
*** Elite Management Before Autocratic Leader Succession: Evidence from North Korea
*** Edward Goldring & Peter Ward
*** Replication Code
*** December 2023
************************************************************************************



******************
*** Set directory
******************
*cd ""



*****************
*** Load dataset
*****************
use "EMBALS_WorldPolitics.dta", clear



******************************************************************
*** Figure 1: Number of military and other events attended by KJI
******************************************************************
preserve
	gen year=year(date)
	keep eventID sector year
	duplicates drop
	gen n=1
	bys year: egen total=total(n)
	replace sector="O" if sector!="M"
	bys sector year: egen total_events=total(n)
	sort eventID
	separate total_events, by(sector=="M")
	graph bar (mean) total total_events0 total_events1, over(year, lab(labsize(small) angle(45))) ///
		scheme(s1mono) ///
		bar(1, color(gs12)) ///
		bar(2, color(gs6)) ///
		bar(3, color(gs0)) ///		
		legend(pos(6) cols(3) region(col(white)) label(1 "All events") label(2 "Non-military events") label(3 "Military events")) ///
		ytitle("Number of events", size(medium)) ///
		ylab(, tlength(0) labsize(small))
restore



*****************************************************************************
*** Table 1: Difference in means of who attends events pre- and post-stroke
*****************************************************************************
preserve
	keep if attend_one!=1
	keep if treatment==0
	gen count=1
	bys elite_id (modate eventID): egen total_events=total(count) 
	bys elite_id (modate eventID): egen total_attend=total(attend) 
	gen prop_attend=(total_attend/total_events)*100
	keep elite_id pi_cabinet pi_military pi_party prop_attend
	duplicates drop
	gen pre=1
	tempfile temp_pre
	save `temp_pre'
		keep if pi_cabinet==1
		keep pre prop_attend
		tempfile pre_cab
		save `pre_cab'
	use `temp_pre'
		keep if pi_military==1
		keep pre prop_attend
		tempfile pre_mil
		save `pre_mil'
	use `temp_pre'
		keep if pi_party==1
		keep pre prop_attend
		tempfile pre_par
		save `pre_par'
restore
	
preserve
	keep if attend_one!=1
	keep if treatment==1
	gen count=1
	bys elite_id (modate eventID): egen total_events=total(count) 
	bys elite_id (modate eventID): egen total_attend=total(attend) 
	gen prop_attend=(total_attend/total_events)*100
	keep elite_id pi_cabinet pi_military pi_party prop_attend
	duplicates drop
	gen post=1
	tempfile temp_post
	save `temp_post'
		keep if pi_cabinet==1
		keep post prop_attend
		append using `pre_cab'
		gen cat=""
		replace cat="pre" if pre==1
		replace cat="post" if post==1
		sort cat
		ttest prop_attend, by(cat)
	use `temp_post', clear
		keep if pi_military==1
		keep post prop_attend
		append using `pre_mil'
		gen cat=""
		replace cat="pre" if pre==1
		replace cat="post" if post==1
		sort cat
		ttest prop_attend, by(cat)
	use `temp_post', clear
		keep if pi_party==1
		keep post prop_attend
		append using `pre_par'
		gen cat=""
		replace cat="pre" if pre==1
		replace cat="post" if post==1
		sort cat
		ttest prop_attend, by(cat)
restore



*********************************************
*** Figure 2: Substantive Effects of Models
*********************************************
preserve
	set seed 975312468
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 3 "Cabinet, pre-stroke" 5 "Cabinet, post-stroke" 8 "Military, pre-stroke" 10 "Military, post-stroke" 13 "Party, pre-stroke" 15 "Party, post-stroke" 20 "Cabinet, FD" 23 "Party, FD" 26 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3
	
	setx treatment 1 pi_cabinet 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5

	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0090628 if n==20
	replace upperci = .0073603 if n==20
	replace estimate = .0019464 if n==20
	
	drop b1-b10
	drop interaction	
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx treatment 1 pi_military 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==10
	replace upperci = r(r2) if n==10
	sum pi
	replace estimate = r(mean) if n==10
	drop pi
	replace estimate=1-estimate if n==10
	replace lowerci=1-lowerci if n==10
	replace upperci=1-upperci if n==10

	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0599666 if n==23
	replace upperci = .0155869 if n==23
	replace estimate = -.0209513 if n==23
	
	drop b1-b10
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==13
	replace upperci = r(r2) if n==13
	sum pi
	replace estimate = r(mean) if n==13
	drop pi
	replace estimate=1-estimate if n==13
	replace lowerci=1-lowerci if n==13
	replace upperci=1-upperci if n==13
	
	setx treatment 1 pi_party 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==15
	replace upperci = r(r2) if n==15
	sum pi
	replace estimate = r(mean) if n==15
	drop pi
	replace estimate=1-estimate if n==15
	replace lowerci=1-lowerci if n==15
	replace upperci=1-upperci if n==15
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .002833 if n==26
	replace upperci = .0496274 if n==26
	replace estimate = .022754 if n==26
		
	twoway (rspike upperci lowerci variable if variable<29, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<29, mcolor(black) msymbol(O)), ///
	xline(17.5, lcolor(gs14)) ///
	legend(off) ///
	title("		Predicted probabilities				First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(3 "Cabinet, pre" 5 "Cabinet, post" 8 "Military, pre" 10 "Military, post" 13 "Party, pre" 15 "Party, post" 20 "Cabinet" 23 "Military" 26 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(pp_fd, replace)
restore



*****************************************
*** Figure 3: Placebo tests
*****************************************
preserve
	set seed 975312468
	gen consolidation=0
	replace consolidation=1 if date>16070
	gen cons_int=pi_cabinet*consolidation
	estsimp logit attend pi_cabinet consolidation cons_int female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)	
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 4 "Cabinet, pre-stroke" 7 "Cabinet, post-stroke" 12 "Military, pre-stroke" 15 "Military, post-stroke" 20 "Party, pre-stroke" 23 "Party, post-stroke" 31 "Cabinet, FD" 36 "Military, FD" 41 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx pi_cabinet 1 consolidation 0 cons_int 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==4
	replace upperci = r(r2) if n==4
	sum pi
	replace estimate = r(mean) if n==4
	drop pi
	replace estimate=1-estimate if n==4
	replace lowerci=1-lowerci if n==4
	replace upperci=1-upperci if n==4
	
	setx pi_cabinet 1 consolidation 1 cons_int 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==7
	replace upperci = r(r2) if n==7
	sum pi
	replace estimate = r(mean) if n==7
	drop pi
	replace estimate=1-estimate if n==7
	replace lowerci=1-lowerci if n==7
	replace upperci=1-upperci if n==7

	setx pi_cabinet 1 consolidation 0 cons_int 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(consolidation 0 1 cons_int 0 1)
	replace lowerci = -.013438 if n==31
	replace upperci = .0114051 if n==31
	replace estimate = -.0014764 if n==31
	
	drop b1-b10
	drop cons_int
	gen cons_int=pi_military*consolidation
	estsimp logit attend pi_military consolidation cons_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	setx pi_military 1 consolidation 0 cons_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==12
	replace upperci = r(r2) if n==12
	sum pi
	replace estimate = r(mean) if n==12
	drop pi
	replace estimate=1-estimate if n==12
	replace lowerci=1-lowerci if n==12
	replace upperci=1-upperci if n==12
	
	setx pi_military 1 consolidation 1 cons_int 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==15
	replace upperci = r(r2) if n==15
	sum pi
	replace estimate = r(mean) if n==15
	drop pi
	replace estimate=1-estimate if n==15
	replace lowerci=1-lowerci if n==15
	replace upperci=1-upperci if n==15

	setx pi_military 1 consolidation 0 cons_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(consolidation 0 1 cons_int 0 1)
	replace lowerci = -.0544686 if n==36
	replace upperci = .0201371 if n==36
	replace estimate = -.0202791 if n==36

	drop b1-b10
	drop cons_int
	gen cons_int=pi_party*consolidation
	estsimp logit attend pi_party consolidation cons_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	setx pi_party 1 consolidation 0 cons_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==20
	replace upperci = r(r2) if n==20
	sum pi
	replace estimate = r(mean) if n==20
	drop pi
	replace estimate=1-estimate if n==20
	replace lowerci=1-lowerci if n==20
	replace upperci=1-upperci if n==20
	
	setx pi_party 1 consolidation 1 cons_int 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==23
	replace upperci = r(r2) if n==23
	sum pi
	replace estimate = r(mean) if n==23
	drop pi
	replace estimate=1-estimate if n==23
	replace lowerci=1-lowerci if n==23
	replace upperci=1-upperci if n==23

	setx pi_party 1 consolidation 0 cons_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(consolidation 0 1 cons_int 0 1)
	replace lowerci = -.0205467 if n==41
	replace upperci = .0243801 if n==41
	replace estimate = .0020756 if n==41
	
	
	drop b1-b10
	gen nuclear=0
	replace nuclear=1 if date>17080
	gen nuc_int=pi_cabinet*nuclear
	estsimp logit attend pi_cabinet nuclear nuc_int female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)	

	setx pi_cabinet 1 nuclear 0 nuc_int 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5
	
	setx pi_cabinet 1 nuclear 1 nuc_int 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8

	setx pi_cabinet 1 nuclear 0 nuc_int 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(nuclear 0 1 nuc_int 0 1)
	replace lowerci = -.0178247 if n==32
	replace upperci = -.0007815 if n==32
	replace estimate = -.0090253 if n==32
	
	drop b1-b10
	drop nuc_int
	gen nuc_int=pi_military*nuclear
	estsimp logit attend pi_military nuclear nuc_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	setx pi_military 1 nuclear 0 nuc_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==13
	replace upperci = r(r2) if n==13
	sum pi
	replace estimate = r(mean) if n==13
	drop pi
	replace estimate=1-estimate if n==13
	replace lowerci=1-lowerci if n==13
	replace upperci=1-upperci if n==13
	
	setx pi_military 1 nuclear 1 nuc_int 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==16
	replace upperci = r(r2) if n==16
	sum pi
	replace estimate = r(mean) if n==16
	drop pi
	replace estimate=1-estimate if n==16
	replace lowerci=1-lowerci if n==16
	replace upperci=1-upperci if n==16

	setx pi_military 1 nuclear 0 nuc_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(nuclear 0 1 nuc_int 0 1)
	replace lowerci = -.1002844 if n==37
	replace upperci = -.0106711 if n==37
	replace estimate = -.0500847 if n==37

	drop b1-b10
	drop nuc_int
	gen nuc_int=pi_party*nuclear
	estsimp logit attend pi_party nuclear nuc_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	setx pi_party 1 nuclear 0 nuc_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==21
	replace upperci = r(r2) if n==21
	sum pi
	replace estimate = r(mean) if n==21
	drop pi
	replace estimate=1-estimate if n==21
	replace lowerci=1-lowerci if n==21
	replace upperci=1-upperci if n==21
	
	setx pi_party 1 nuclear 1 nuc_int 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==24
	replace upperci = r(r2) if n==24
	sum pi
	replace estimate = r(mean) if n==24
	drop pi
	replace estimate=1-estimate if n==24
	replace lowerci=1-lowerci if n==24
	replace upperci=1-upperci if n==24

	setx pi_party 1 nuclear 0 nuc_int 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(nuclear 0 1 nuc_int 0 1)
	replace lowerci = -.0213672 if n==42
	replace upperci = .0136745 if n==42
	replace estimate = -.0052394 if n==42
	
	gen cons=0
	replace cons=1 if n==4
	replace cons=1 if n==7
	replace cons=1 if n==12
	replace cons=1 if n==15
	replace cons=1 if n==20
	replace cons=1 if n==23
	replace cons=1 if n==31
	replace cons=1 if n==36
	replace cons=1 if n==41

	
	twoway (rspike upperci lowerci variable if variable<45, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<45 & cons==1, mcolor(black) msymbol(O)) ///
	(scatter estimate variable if variable<45 & cons==0, mcolor(black) msymbol(X)), ///
	xline(27.5, lcolor(gs14)) ///
	legend(pos(6) fcolor(white) order(2 "Consolidation placebo" 3 "Nuclear placebo") rows(1) size(*0.8) region(lstyle(none))) ///
	title("		Predicted probabilities					First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(4.5 "Cabinet, pre" 7.5 "Cabinet, post" 12.5 "Military, pre" 15.5 "Military, post" 20.5 "Party, pre" 23.5 "Party, post" 31.5 "Cabinet" 36.5 "Military" 41.5 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(placebos, replace)
restore



***************************************
*** Identifying KWP elites for Table 2
***************************************
preserve
	gen counter=1
	bys elite_id : egen pre_attend=sum(attend) if treatment==0
	bys elite_id : egen pre_events=sum(counter) if treatment==0
	keep elite_id pre_attend pre_events
	drop if pre_attend==.
	duplicates drop
	sort elite_id
	tempfile pre
	save `pre'
restore
	gen counter=1
	bys elite_id : egen post_attend=sum(attend) if treatment==1 //no. events attended post-stroke
	bys elite_id : egen post_events=sum(counter) if treatment==1 //no. events post-stroke
	
	bys elite_id : egen total_party=sum(pi_party) if treatment==1 //no. events they were KWP
	bys elite_id : egen total_events=sum(counter) if treatment==1 //no. events post-stroke
	gen prop_party=((total_party/total_events)*100) if treatment==1 //prop. events they were KWP
	
	keep elite_id post_attend post_events prop_party
	drop if post_attend==.
	duplicates drop
	sort elite_id
	merge 1:1 elite_id using `pre'
	drop if _merge==2
	drop _merge
	gen p_value=.

	levelsof elite_id, local(levels)
	foreach l of local levels {
		di `l'
	
preserve
	keep if elite_id==`l'

	local pre_attend=pre_attend
	local post_attend=post_attend
	local pre_events=pre_events
	local post_events=post_events
	iri `pre_attend' `post_attend' `pre_events' `post_events'

	return list
restore
	
	replace p_value=r(p_twosided_midp) if elite_id==`l'
}
	gen significant=.
	replace significant=1 if p_value<0.05

gen prop_pre= ((pre_attend/pre_events)*100)	
gen prop_post= ((post_attend/post_events)*100)
gen more_after=1 if prop_post>prop_pre

keep if significant==1 & more_after==1 //elites who were significantly more likely to attend an event after the stroke
drop if prop_party<50
sort prop_party
use "EMBALS_WorldPolitics.dta", clear



***************************************
*** Identifying KWP elites for Table 3
***************************************
preserve
	gen counter=1
	bys elite_id : egen pre_attend=sum(attend) if treatment==0
	bys elite_id : egen pre_events=sum(counter) if treatment==0
	keep elite_id pre_attend pre_events
	drop if pre_attend==.
	duplicates drop
	sort elite_id
	tempfile pre
	save `pre'
restore
	gen counter=1
	bys elite_id : egen post_attend=sum(attend) if treatment==1 //no. events attended post-stroke
	bys elite_id : egen post_events=sum(counter) if treatment==1 //no. events post-stroke
	
	bys elite_id : egen total_party=sum(pi_party) if treatment==1 //no. events they were KWP
	bys elite_id : egen total_events=sum(counter) if treatment==1 //no. events post-stroke
	gen prop_party=((total_party/total_events)*100) if treatment==1 //prop. events they were KWP
	
	keep elite_id post_attend post_events prop_party
	drop if post_attend==.
	duplicates drop
	sort elite_id
	merge 1:1 elite_id using `pre'
	drop if _merge==2
	drop _merge
	gen p_value=.

	levelsof elite_id, local(levels)
	foreach l of local levels {
		di `l'
	
preserve
	keep if elite_id==`l'

	local pre_attend=pre_attend
	local post_attend=post_attend
	local pre_events=pre_events
	local post_events=post_events
	iri `pre_attend' `post_attend' `pre_events' `post_events'

	return list
restore
	
	replace p_value=r(p_twosided_midp) if elite_id==`l'
}
	gen significant=.
	replace significant=1 if p_value<0.05

gen prop_pre= ((pre_attend/pre_events)*100)	
gen prop_post= ((post_attend/post_events)*100)
gen more_after=1 if prop_post>prop_pre

drop if prop_party<50
drop if more_after==1
sort prop_party
use "EMBALS_WorldPolitics.dta", clear



****************************
****************************
****************************
*** Supplementary Materials
****************************
****************************
****************************



********************************
*** Table C1: Summary Statistics
********************************
preserve
	gen consolidation=0
	replace consolidation=1 if date>16070
	gen nuclear=0
	replace nuclear=1 if date>17080

	sum attend pi_cabinet pi_military pi_party treatment female age month consolidation nuclear, det
	tab attend, m
	tab pi_cabinet, m
	tab pi_military, m
	tab pi_party, m
	tab treatment, m
	tab female, m
	tab consolidation, m
	tab nuclear, m
restore



***************************
*** Table D1: Main Results
***************************
preserve
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)
	drop interaction	
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	drop interaction	
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
restore



*******************************************
*** Figure E1: Kim Jong Il's consolidation
*******************************************
preserve
	import delimited using "gandhi-sumner-estimates.csv", clear
	encode countryleaderspan, generate(leader)
	twoway (line xhatmean year if leader==326, lpattern(solid) lcolor(gray)), ///
		scheme(s1mono) ///
		ytitle("Consolidation (Gandhi and Sumner 2020)", size(medium)) ///
		xtitle("Year", size(medium)) ///
		ylabel(, tlength(0) labsize(small)) ///
		xlabel(, tlength(0) labsize(small)) ///
		legend(off) ///
		name(gs_cons, replace)
restore



****************************
*** Table F1: Placebo Tests
****************************
* Consolidation placebo test
preserve
	gen consolidation=0
	replace consolidation=1 if date>16070
	gen cons_int=pi_cabinet*consolidation
	logit attend pi_cabinet consolidation cons_int female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)	
	
	drop cons_int
	gen cons_int=pi_military*consolidation
	logit attend pi_military consolidation cons_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	drop cons_int
	gen cons_int=pi_party*consolidation
	logit attend pi_party consolidation cons_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
restore

* Nuclear placebo test
preserve
	gen nuclear=0
	replace nuclear=1 if date>17080
	gen nuc_int=pi_cabinet*nuclear
	logit attend pi_cabinet nuclear nuc_int female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)	

	drop nuc_int
	gen nuc_int=pi_military*nuclear
	logit attend pi_military nuclear nuc_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)

	drop nuc_int
	gen nuc_int=pi_party*nuclear
	logit attend pi_party nuclear nuc_int female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
restore



**********************
*** Robustness Tests
**********************
* Elite idiosyncratic characteristics
preserve
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb i.elite_id if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb i.elite_id if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb i.elite_id if attend_one!=1, cluster(elite_id)
restore



* Including elites who attended just one event
preserve
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb, cluster(elite_id)
	drop interaction	
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb, cluster(elite_id)
	drop interaction
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb, cluster(elite_id)
restore

preserve
	set seed 975312468
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb, cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 2 "Cabinet, pre-stroke" 3 "Cabinet, post-stroke" 5 "Military, pre-stroke" 6 "Military, post-stroke" 8 "Party, pre-stroke" 9 "Party, post-stroke" 12 "Cabinet, FD" 13 "Party, FD" 14 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==2
	replace upperci = r(r2) if n==2
	sum pi
	replace estimate = r(mean) if n==2
	drop pi
	replace estimate=1-estimate if n==2
	replace lowerci=1-lowerci if n==2
	replace upperci=1-upperci if n==2
	
	setx treatment 1 pi_cabinet 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3

	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0074259 if n==12
	replace upperci = .0033835 if n==12
	replace estimate = -.0025963 if n==12
	
	drop b1-b10
	drop interaction	
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb, cluster(elite_id)
	
	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5
	
	setx treatment 1 pi_military 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==6
	replace upperci = r(r2) if n==6
	sum pi
	replace estimate = r(mean) if n==6
	drop pi
	replace estimate=1-estimate if n==6
	replace lowerci=1-lowerci if n==6
	replace upperci=1-upperci if n==6

	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0464427 if n==13
	replace upperci = .0133733 if n==13
	replace estimate = -.0154932 if n==13
	
	drop b1-b10
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb, cluster(elite_id)
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx treatment 1 pi_party 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==9
	replace upperci = r(r2) if n==9
	sum pi
	replace estimate = r(mean) if n==9
	drop pi
	replace estimate=1-estimate if n==9
	replace lowerci=1-lowerci if n==9
	replace upperci=1-upperci if n==9
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .0028269 if n==14
	replace upperci = .0442664 if n==14
	replace estimate = .0204527 if n==14
		
	twoway (rspike upperci lowerci variable if variable<16, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<16, mcolor(black) msymbol(O)), ///
	legend(off) ///
	title("			Predicted probabilities					First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(2 "Cabinet, pre" 3 "Cabinet, post" 5 "Military, pre" 6 "Military, post" 8 "Party, pre" 9 "Party, post" 12 "Cabinet" 13 "Military" 14 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(one_event, replace)
restore



* Past attendance
preserve
	bys elite_id (date eventID): gen lag_attend=attend[_n-1]
	sort date eventID elite_id
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
restore



preserve
	set seed 975312468
	bys elite_id (date eventID): gen lag_attend=attend[_n-1]
	sort date eventID elite_id
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 2 "Cabinet, pre-stroke" 3 "Cabinet, post-stroke" 5 "Military, pre-stroke" 6 "Military, post-stroke" 8 "Party, pre-stroke" 9 "Party, post-stroke" 12 "Cabinet, FD" 13 "Military, FD" 14 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx pi_cabinet 1 treatment 0 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==2
	replace upperci = r(r2) if n==2
	sum pi
	replace estimate = r(mean) if n==2
	drop pi
	replace estimate=1-estimate if n==2
	replace lowerci=1-lowerci if n==2
	replace upperci=1-upperci if n==2
	
	setx pi_cabinet 1 treatment 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3

	setx pi_cabinet 1 treatment 0 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean lag_attend 0
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0045593 if n==12
	replace upperci = .0054567 if n==12
	replace estimate = -.0003442 if n==12
	
	drop b1-b11
	drop interaction
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
	
	setx pi_military 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5
	
	setx pi_military 1 treatment 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==6
	replace upperci = r(r2) if n==6
	sum pi
	replace estimate = r(mean) if n==6
	drop pi
	replace estimate=1-estimate if n==6
	replace lowerci=1-lowerci if n==6
	replace upperci=1-upperci if n==6

	setx pi_military 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0242638 if n==13
	replace upperci = .0053151 if n==13
	replace estimate = -.0084131 if n==13
	
	drop b1-b11
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb lag_attend if attend_one!=1, cluster(elite_id)
	
	setx pi_party 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx pi_party 1 treatment 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==9
	replace upperci = r(r2) if n==9
	sum pi
	replace estimate = r(mean) if n==9
	drop pi
	replace estimate=1-estimate if n==9
	replace lowerci=1-lowerci if n==9
	replace upperci=1-upperci if n==9

	setx pi_party 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean lag_attend 0
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .0012173 if n==14
	replace upperci = .0224407 if n==14
	replace estimate = .010612 if n==14
		
	twoway (rspike upperci lowerci variable if variable<16, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<16, mcolor(black) msymbol(O)), ///
	legend(off) ///
	title("		Predicted probabilities					First differences", size(medium)) ///
	ylabel(-.1(.05).1, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(2 "Cabinet, pre" 3 "Cabinet, post" 5 "Military, pre" 6 "Military, post" 8 "Party, pre" 9 "Party, post" 12 "Cabinet" 13 "Military" 14 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(past_attend, replace)
restore



* Age robustness check
preserve
	qui sum age if attend==1 & eng_nam!="Kim Jong Un", det
	qui return list
	drop if age<(`r(min)'-`r(sd)') & eng_nam!="Kim Jong Un"
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	drop interaction
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
restore


preserve
	set seed 975312468
	qui sum age if attend==1 & eng_nam!="Kim Jong Un", det
	qui return list
	drop if age<(`r(min)'-`r(sd)') & eng_nam!="Kim Jong Un"
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 2 "Cabinet, pre-stroke" 3 "Cabinet, post-stroke" 5 "Military, pre-stroke" 6 "Military, post-stroke" 8 "Party, pre-stroke" 9 "Party, post-stroke" 12 "Cabinet, FD" 13 "Military, FD" 14 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx pi_cabinet 1 treatment 0 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==2
	replace upperci = r(r2) if n==2
	sum pi
	replace estimate = r(mean) if n==2
	drop pi
	replace estimate=1-estimate if n==2
	replace lowerci=1-lowerci if n==2
	replace upperci=1-upperci if n==2
	
	setx pi_cabinet 1 treatment 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3

	setx pi_cabinet 1 treatment 0 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0090034 if n==12
	replace upperci = .0074837 if n==12
	replace estimate = -.0018667 if n==12
	
	drop b1-b10
	drop interaction
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	setx pi_military 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5
	
	setx pi_military 1 treatment 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==6
	replace upperci = r(r2) if n==6
	sum pi
	replace estimate = r(mean) if n==6
	drop pi
	replace estimate=1-estimate if n==6
	replace lowerci=1-lowerci if n==6
	replace upperci=1-upperci if n==6

	setx pi_military 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.05986 if n==13
	replace upperci = .0160202 if n==13
	replace estimate = -.0206749 if n==13
	
	drop b1-b10
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1, cluster(elite_id)
	
	setx pi_party 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx pi_party 1 treatment 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==9
	replace upperci = r(r2) if n==9
	sum pi
	replace estimate = r(mean) if n==9
	drop pi
	replace estimate=1-estimate if n==9
	replace lowerci=1-lowerci if n==9
	replace upperci=1-upperci if n==9

	setx pi_party 1 treatment 0 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .0027715 if n==14
	replace upperci = .0498358 if n==14
	replace estimate = .0227532 if n==14
		
	twoway (rspike upperci lowerci variable if variable<16, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<16, mcolor(black) msymbol(O)), ///
	legend(off) ///
	title("		Predicted probabilities					First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(2 "Cabinet, pre" 3 "Cabinet, post" 5 "Military, pre" 6 "Military, post" 8 "Party, pre" 9 "Party, post" 12 "Cabinet" 13 "Military" 14 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(age_attend, replace)
restore



* Event-based count models
preserve
	bys eventID (elite_id date): egen cabinet_attendees=total(attend) if pi_cabinet==1
	keep eventID date cabinet_attendees treatment month month_sq month_cb
	drop if cabinet_attendees==.
	duplicates drop
	nbreg cabinet_attendees treatment month month_sq month_cb, robust
restore
preserve
	bys eventID (elite_id date): egen military_attendees=total(attend) if pi_military==1
	keep eventID date military_attendees treatment month month_sq month_cb
	drop if military_attendees==.
	duplicates drop
	nbreg military_attendees treatment month month_sq month_cb, robust
restore
preserve
	bys eventID (elite_id date): egen party_attendees=total(attend) if pi_party==1
	keep eventID date party_attendees treatment month month_sq month_cb
	drop if party_attendees==.
	duplicates drop
	nbreg party_attendees treatment month month_sq month_cb, robust
	
	
	set seed 864213579
	estsimp nbreg party_attendees treatment month month_sq month_cb, robust
	setx mean
	setx treatment 0
	simqi
	setx treatment 1
	simqi
restore


* a time-based regression discontinuity design?
use "EMBALS_WorldPolitics.dta", clear

gen running=modate-584

rdbwselect attend running if attend_one!=1 & pi_cabinet==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
rdrobust attend running if attend_one!=1 & pi_cabinet==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
local bandwidth = e(h_l)
local p_value = e(pv_rb)
local rounded_p = round(`p_value',0.01)
di `rounded_p'
rdplot attend running if abs(running) <= `bandwidth' & attend_one!=1 & pi_cabinet==1, binselect(es) kernel(triangular) p(1) h(`bandwidth') vce(cluster elite_id) all graph_options(scheme(s1mono) aspectratio(1) legend(off) title("Cabinet elites", size(medium)) xtitle("Months to stroke") ylab(0(.02).1) ytitle("Pr(Attend = 1)") note("{it:p}: 0`rounded_p'", ring(0) pos(2)) name(rd_cabinet, replace))

rdbwselect attend running if attend_one!=1 & pi_military==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
rdrobust attend running if attend_one!=1 & pi_military==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
local bandwidth = e(h_l)
local p_value = e(pv_rb)
local rounded_p = round(`p_value',0.01)
di `rounded_p'
rdplot attend running if abs(running) <= `bandwidth' & attend_one!=1 & pi_military==1, binselect(es) kernel(triangular) p(1) h(`bandwidth') vce(cluster elite_id) all graph_options(scheme(s1mono) aspectratio(1) legend(off) title("Military elites", size(medium)) xtitle("Months to stroke") ylab(0(.02).1) ytitle("Pr(Attend = 1)") note("{it:p}: 0`rounded_p'", ring(0) pos(2)) name(rd_military, replace))

rdbwselect attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
rdrobust attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) bwselect(mserd) vce(cluster elite_id) all
local bandwidth = e(h_l)
local p_value = e(pv_rb)
local rounded_p = round(`p_value',0.01)
di `rounded_p'
rdplot attend running if abs(running) <= `bandwidth' & attend_one!=1 & pi_party==1, binselect(es) kernel(triangular) p(1) h(`bandwidth') vce(cluster elite_id) all graph_options(scheme(s1mono) aspectratio(1) legend(off) title("Party elites", size(medium)) xtitle("Months to stroke") ylab(0(.02).1) ytitle("Pr(Attend = 1)") note("{it:p}: 0`rounded_p'", ring(0) pos(2)) name(rd_party, replace))

graph combine rd_cabinet rd_military rd_party, ///
	rows(1) ///
	ycommon ///
	graphregion(color(white))

* age and gender controls
rdrobust attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) covs(female age) bwselect(mserd) vce(cluster elite_id) all
* elite idiosyncratic characteristics
tab elite_id, gen(g)
rdrobust attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) covs(female age g1-g230) bwselect(mserd) vce(cluster elite_id) all
* past attendance
bys elite_id (date eventID): gen lag_attend=attend[_n-1]
sort date eventID elite_id
rdrobust attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) covs(female age g1-g230 lag_attend) bwselect(mserd) vce(cluster elite_id) all
* omitting young elites
qui sum age if attend==1 & eng_nam!="Kim Jong Un", det
qui return list
drop if age<(`r(min)'-`r(sd)') & eng_nam!="Kim Jong Un"
rdrobust attend running if attend_one!=1 & pi_party==1, kernel(triangular) p(1) covs(female age g1-g230 lag_attend) bwselect(mserd) vce(cluster elite_id) all



*********************************
*** Probing the argument's logic
*********************************
*military sub-sample
preserve
	gen interaction=pi_cabinet*treatment
	logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
	drop interaction	
	gen interaction=pi_military*treatment
	logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
	drop interaction
	gen interaction=pi_party*treatment
	logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
restore


preserve
	set seed 975312468
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 3 "Cabinet, pre-stroke" 5 "Cabinet, post-stroke" 8 "Military, pre-stroke" 10 "Military, post-stroke" 13 "Party, pre-stroke" 15 "Party, post-stroke" 20 "Cabinet, FD" 23 "Party, FD" 26 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3
	
	setx treatment 1 pi_cabinet 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5

	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0011515 if n==20
	replace upperci = .0085169 if n==20
	replace estimate = .0025152 if n==20
	
	drop b1-b10
	drop interaction	
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
	
	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx treatment 1 pi_military 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==10
	replace upperci = r(r2) if n==10
	sum pi
	replace estimate = r(mean) if n==10
	drop pi
	replace estimate=1-estimate if n==10
	replace lowerci=1-lowerci if n==10
	replace upperci=1-upperci if n==10

	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0568075 if n==23
	replace upperci = .0664125 if n==23
	replace estimate = .0030147 if n==23
	
	drop b1-b10
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb if attend_one!=1 & sector=="M", cluster(elite_id)
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==13
	replace upperci = r(r2) if n==13
	sum pi
	replace estimate = r(mean) if n==13
	drop pi
	replace estimate=1-estimate if n==13
	replace lowerci=1-lowerci if n==13
	replace upperci=1-upperci if n==13
	
	setx treatment 1 pi_party 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==15
	replace upperci = r(r2) if n==15
	sum pi
	replace estimate = r(mean) if n==15
	drop pi
	replace estimate=1-estimate if n==15
	replace lowerci=1-lowerci if n==15
	replace upperci=1-upperci if n==15
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .0055037 if n==26
	replace upperci = .0532895 if n==26
	replace estimate = .0255783 if n==26
		
	twoway (rspike upperci lowerci variable if variable<29, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<29, mcolor(black) msymbol(O)), ///
	xline(17.5, lcolor(gs14)) ///
	legend(off) ///
	title("		Predicted probabilities				First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(3 "Cabinet, pre" 5 "Cabinet, post" 8 "Military, pre" 10 "Military, post" 13 "Party, pre" 15 "Party, post" 20 "Cabinet" 23 "Military" 26 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(pp_fd_m, replace)
restore



* event size
preserve
	bys eventID: egen event_size = total(attend)
	set seed 975312468
	gen interaction=pi_cabinet*treatment
	estsimp logit attend pi_cabinet treatment interaction female age pi_military month month_sq month_cb event_size if attend_one!=1, cluster(elite_id)
	
	gen estimate=.
	gen upperci=.
	gen lowerci=.
	gen variable=_n
	label define IVS 3 "Cabinet, pre-stroke" 5 "Cabinet, post-stroke" 8 "Military, pre-stroke" 10 "Military, post-stroke" 13 "Party, pre-stroke" 15 "Party, post-stroke" 20 "Cabinet, FD" 23 "Party, FD" 26 "Party, FD"
	label value variable IVS
	gen n=_n
	
	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==3
	replace upperci = r(r2) if n==3
	sum pi
	replace estimate = r(mean) if n==3
	drop pi
	replace estimate=1-estimate if n==3
	replace lowerci=1-lowerci if n==3
	replace upperci=1-upperci if n==3
	
	setx treatment 1 pi_cabinet 1 interaction 1 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==5
	replace upperci = r(r2) if n==5
	sum pi
	replace estimate = r(mean) if n==5
	drop pi
	replace estimate=1-estimate if n==5
	replace lowerci=1-lowerci if n==5
	replace upperci=1-upperci if n==5

	setx treatment 0 pi_cabinet 1 interaction 0 female 0 age mean pi_military 0 month mean month_sq mean month_cb mean event_size mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0090628 if n==20
	replace upperci = .0073603 if n==20
	replace estimate = .0019464 if n==20
	
	drop b1-b11
	drop interaction	
	gen interaction=pi_military*treatment
	estsimp logit attend pi_military treatment interaction female age pi_cabinet month month_sq month_cb event_size if attend_one!=1, cluster(elite_id)
	
	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==8
	replace upperci = r(r2) if n==8
	sum pi
	replace estimate = r(mean) if n==8
	drop pi
	replace estimate=1-estimate if n==8
	replace lowerci=1-lowerci if n==8
	replace upperci=1-upperci if n==8
	
	setx treatment 1 pi_military 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==10
	replace upperci = r(r2) if n==10
	sum pi
	replace estimate = r(mean) if n==10
	drop pi
	replace estimate=1-estimate if n==10
	replace lowerci=1-lowerci if n==10
	replace upperci=1-upperci if n==10

	setx treatment 0 pi_military 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = -.0599666 if n==23
	replace upperci = .0155869 if n==23
	replace estimate = -.0209513 if n==23
	
	drop b1-b11
	drop interaction
	gen interaction=pi_party*treatment
	estsimp logit attend pi_party treatment interaction female age pi_cabinet month month_sq month_cb event_size if attend_one!=1, cluster(elite_id)
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==13
	replace upperci = r(r2) if n==13
	sum pi
	replace estimate = r(mean) if n==13
	drop pi
	replace estimate=1-estimate if n==13
	replace lowerci=1-lowerci if n==13
	replace upperci=1-upperci if n==13
	
	setx treatment 1 pi_party 1 interaction 1 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	local mean = r(mean)
	simqi, genpr(pi)
	_pctile pi, p(2.5,97.5)
	replace lowerci = r(r1) if n==15
	replace upperci = r(r2) if n==15
	sum pi
	replace estimate = r(mean) if n==15
	drop pi
	replace estimate=1-estimate if n==15
	replace lowerci=1-lowerci if n==15
	replace upperci=1-upperci if n==15
	
	setx treatment 0 pi_party 1 interaction 0 female 0 age mean pi_cabinet 0 month mean month_sq mean month_cb mean event_size mean
	simqi, fd(prval(1)) changex(treatment 0 1 interaction 0 1)
	replace lowerci = .002833 if n==26
	replace upperci = .0496274 if n==26
	replace estimate = .022754 if n==26
		
	twoway (rspike upperci lowerci variable if variable<29, msize(vtiny) lcolor(black)) ///
	(scatter estimate variable if variable<29, mcolor(black) msymbol(O)), ///
	xline(17.5, lcolor(gs14)) ///
	legend(off) ///
	title("		Predicted probabilities				First differences", size(medium)) ///
	ylabel(-.15(.05).15, tlength(0)) ///
	xtitle("") ///
	yline(0, lcolor(gs10) lpattern(dash)) ///
	xlabel(3 "Cabinet, pre" 5 "Cabinet, post" 8 "Military, pre" 10 "Military, post" 13 "Party, pre" 15 "Party, post" 20 "Cabinet" 23 "Military" 26 "Party", valuelabel tlength(0) angle(45)) ///
	scheme(s1mono) ///
	name(event_size, replace)
restore


